﻿import { generalRemove, commonWarehouseOverviewFind, packageCodeConvert, dictionaryDataFind } from '@/api/common'

// api 批量删除方法
/*
* data 删除的的 ID 字符串
*
* location  删除接口的位置
*
* */
export function generalRemoveR(data, location, self) {
  self.$confirm(self.$t('common.permanentlyDelete'), self.$t('common.prompt'), {
    confirmButtonText: self.$t('common.confirm'),
    cancelButtonText: self.$t('common.cancel'),
    type: 'warning'
  }).then(() => {
    var dataTemp = []
    for (var i = 0; i < data.length; i++) {
      dataTemp.push(data[i].id)
    }
    var ids = {
      ids: dataTemp.join()
    }
    console.log(ids)
    generalRemove(ids, location).then(response => {
      console.log(response)
      if (response.data.code === '0000') {
        // this.dialogFormVisible = false
        self.$notify({
          title: self.$t('common.succeed'),
          message: self.$t('common.del') + self.$t('common.success'),
          type: 'success',
          duration: 2000
        })
        self.handleFilter()
      } else {
        if (response.data.code === '5000') {
          self.$notify({
            title: self.$t('common.failed'),
            message: '系统错误,请联系管理员',
            type: 'info',
            duration: 2000
          })
        } else {
          self.$notify({
            title: self.$t('common.failed'),
            message: response.data.content,
            type: 'info',
            duration: 2000
          })
        }
      }
    })
  }).catch(() => {
    // this.getList()
    self.$message({
      type: 'info',
      message: self.$t('common.cancelDelete')
    })
  })
}

/*
* 仓库 -- 区域 -- 库区 -- 库位 反查
* 知道某一个ID 查询出 上级 上上级 的ID ; 比如 传库区ID ,  查出来 仓库ID 区域ID 本身
* */

export function findWarehouseOverview(data) {
  var viewType = data.viewType
  var id = data.id
  return new Promise((resolve, reject) => {
    var data = {
      viewType
    }
    data[viewType + 'Id'] = id
    commonWarehouseOverviewFind(data).then(response => {
      var data = response.data.data[viewType]

      var list = []
      switch (viewType) {
        case 'warehouse':
          list.push(data)
          break
        case 'zoneGroup':
          list.push(data.warehouse)

          var zoneGroup = data
          delete zoneGroup.warehouse
          delete zoneGroup.zoneGroup
          delete zoneGroup.zone
          list.push(zoneGroup)
          break
        case 'zone':
          list.push(data.warehouse)
          list.push(data.zoneGroup)

          var zone = data
          delete zone.warehouse
          delete zone.zoneGroup
          delete zone.zone
          list.push(zone)
          break
        case 'location':
          list.push(data.warehouse)
          list.push(data.zoneGroup)
          list.push(data.zone)

          var location = data
          delete location.warehouse
          delete location.zoneGroup
          delete location.zone
          list.push(location)
      }
      resolve(list)
    })
  })
}

// 单位转换
export function _packageCodeConvert(data) {
  return new Promise((resolve, reject) => {
    packageCodeConvert(data).then(response => {
      var quantity = response.data.data.quantity
      resolve(quantity)
    })
  })
}

// 查询数据字典
export function _findDictionaryData(code) {
  var data = {
    code
  }
  return new Promise((resolve, rej) => {
    dictionaryDataFind(data).then(response => {
      var dictionaryData = response.data.data.dictionaryData
      if (dictionaryData) {
        resolve(dictionaryData)
      } else {
        var aa = ''
        resolve(aa)
      }
    })
  })
}
